import java.util.Scanner;

public class Game {
    private char[][] field;
    private int col; // столбец
    private int row; // ряд
    private int hod;
    private int hodMax;
    private boolean isWin = false;
    private boolean krestiki = false;
    Scanner scanner = new Scanner(System.in);

    public Game(int col, int row) {
        this.col = col;
        this.row = row;
        this.field = new char[col][row];
        this.hodMax = col*row;
    }

    protected void run() {
        initField();
        printField();
        play();
    }

    private void initField() {
        for (int r = 0; r < row; r++) {
            for (int c = 0; c < col; c++) {
                field[c][r] = ' ';
            }
        }
    }

    private void printField() {
        System.out.print("   ");
        for (int i = 0; i < col; i++) {
            System.out.print((i+1) + "  ");
        }
        System.out.println();
        for (int r = 0; r < row; r++) {
            System.out.print((r+1) + " ");
            for (int c = 0; c < col; c++) {
                System.out.print("[" + this.field[c][r] + "]");
            }
            System.out.println();
        }
    }

    private void play() {
        System.out.print("Первый ход у: (0 или X): ");
        char which_play = scanner.next().charAt(0);

        if (which_play == 'X') {
            krestiki = true;
        }

        while(!isWin) {
            String question = krestiki ? "Крестики делают свой ход!" : "Нолики делают свой ход!";
            System.out.println(question);

            System.out.print("Какая строка?: ");
            int col1 = scanner.nextInt();
            System.out.print("Какой столбец?: ");
            int row1 = scanner.nextInt();

			if (field[row1-1][col1-1] != ' ') {
				System.out.println("Поле занято!");
				continue;
			}
            if (krestiki) {
                field[row1-1][col1-1] = 'X';
                printField();
                if (Table.check(field)) {
                    isWin = true;
                    System.out.println("Выиграли крестики!");
                }
                krestiki = false;
            } else {
                field[row1-1][col1-1] = '0';
                printField();
                if (Table.check(field)) {
                    isWin = true;
                    System.out.println("Выиграли нолики!");
                }
                krestiki = true;
            }

            hod += 1;
            if(hod == hodMax) {
                System.out.println("Ничья!");
                break;
            }
        }
    }
}